<?php

/**
 * ContactForm class.
 * ContactForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class Invest extends ActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function tableName()
	{
		return 'vh_invest';
	}
	
	public function rules()
	{
		return array(
		);
	}

	public function search()
	{
		$condition = "";
		
		if(!empty($_POST['domain']))
			$condition .= " AND h.domain LIKE '%" . $_POST['domain'] . "%'";
		
		if(!empty($_POST['status']))
			$condition .= " AND h.status = '" . $_POST['status'] . "'";
		
                if($_POST['isFinished'] != "")
			$condition .= " AND t.isFinished = '" . $_POST['isFinished'] . "'";
                
		$query = 'SELECT t.id, h.domain, h.status, t.deposit, t.profitDaily, t.duration, t.planWithdraw, t.withdraw, t.investedDate
			FROM vh_invest t
                        JOIN vh_hyip h ON h.id = t.hyipId
			WHERE 1 = 1 ' . $condition;
		
		$result = $this -> executeAllTotal($query, array(), true, "t.investedDate ASC");
	
		return $result;
	}
        
        public function updateWithdraw($investId)
        {
            $query = '
                    UPDATE vh_invest 
                    SET withdraw = (SELECT SUM(withdraw) - SUM(fee) FROM vh_transaction WHERE investId = :investId)
                    WHERE id = :investId
            ';
            
            $result = $this ->executeNone($query, array(":investId" => $investId));

            return $result;
        }
        
        public function createTable()
	{
            $query = "
                CREATE TABLE `vh_invest` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `hyipId` int(11) NOT NULL COMMENT 'Hyip_id',
                `deposit` float NOT NULL DEFAULT '0',
                `profitDaily` float NOT NULL DEFAULT '0' COMMENT '% lãi hàng ngày',
                `duration` int(11) NOT NULL DEFAULT '0' COMMENT 'Thời gian invest (day)',
                `planWithdraw` float NOT NULL DEFAULT '0',
                `withdraw` float NOT NULL DEFAULT '0',
                `investedDate` datetime DEFAULT NULL,
                PRIMARY KEY (`id`)
              ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
            ";
            
            $result = $this -> executeNone($query);

            return $result;
	}
        
        public function getSummary()
        {					
            $query = 
                "SELECT SUM(depositTotal) as depositTotal, SUM(withdrawTotal) as withdrawTotal, SUM(currentProfit) as currentProfit
				FROM (
					SELECT SUM(deposit) as depositTotal, SUM(withdraw) as withdrawTotal, 0 as currentProfit
					FROM vh_invest				
					UNION ALL
					SELECT 0 as depositTotal, 0 as withdrawTotal, (SUM(withdraw) - SUM(deposit)) as currentProfit
					FROM vh_invest
					WHERE isFinished = 1) as t
				";
            
            $result = $this ->executeRow($query);
            
            return $result;
        }
        
}